<?php
namespace backend\controllers;

use backend\models\Information;
use common\components\BaseController;
use Yii;
use common\helps\tools;
use common\helps\code;
/**
 * Team controller
 */
class InfoController extends BaseController
{

    public $modelClass = 'common\models\Information';
    public function init()
    {
        parent::init();
    }

    /**
     * 获取列表
     * @author cpy(1442435897@qq.com)
     * @return json
     */
    public  function  actionGetList(){
        $request =$this->request;
        $model = new Information();
        $params = $request->queryParams;
        $total_data = $model->getList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        foreach($data['data'] as $k => $value){
            if($value['section'] == 1){
                $row = '行业新闻';
            }else{
                $row = '企业公告';
            }
            $data['data'][$k]['section'] = $row;
        }

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','标题','栏目','创建时间'];
            foreach($total_data as $k => $value){
                if($value['section'] == 1){
                    $row = '行业新闻';
                }else{
                    $row = '企业公告';
                }
                $total_data[$k]['section'] = $row;
                unset($total_data[$k]['status']);
            }
            tools::ToPHPEXECL($tableHeader,$total_data,'资讯列表');
        }
        return code::success($data);
    }

    /**
     * 新增
     * @author cpy(1442435897@qq.com)
     * @return json
     */
    public  function  actionAddInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Information();
            $params = $request->bodyParams;
            $model->scenario='create';
            $model->load($params,'');
            if(!$model->validate()){
                $error = array_values($model->getErrors());
                return code::error('error',$error[0][0]);
            }
            $data=$model->addInfo($params);
            if(!empty($data['error'])){
                return code::error('error',$data['error']);
            }else{
                return code::success();
            }
        }
    }

    /**
     * 详情
     * @author cpy(1442435897@qq.com)
     * @return json
     */
    public  function  actionDetail(){
        $request =$this->request;
        $params = $request->bodyParams;
        $model = new Information();

        if(empty($params['id']))
            return code::error('mistake');
        $res = $model::findOne(['id' => $params['id']]);
        return code::success($res);
    }

    /**
     * 编辑
     * @author cpy(1442435897@qq.com)
     * @return json
     */
    public  function  actionEditInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Information();
            $params = $request->bodyParams;
            $model->scenario='update';
            $model->load($params,'');
            if(!$model->validate()){
                $error = array_values($model->getErrors());
                return code::error('',$error[0][0]);
            }
            $data=$model->editInfo($params);
            if(!empty($data['error'])){
                return code::error('error',$data['error']);
            }else{
                return code::success();
            }
        }
    }

    /**
     * 删除
     * @author cpy(1442435897@qq.com)
     * @return json
     */
    public  function  actionDeleteInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Information();
            $params = $request->bodyParams;
            $model->scenario='update_status';
            $model->load($params,'');
            if(!$model->validate()){
                $error = array_values($model->getErrors());
                return code::error('error',$error[0][0]);
            }
            $data=$model->deleteInfo($params);
            if(!empty($data['error'])){
                return code::error('error',$data['error']);
            }else{
                return code::success();
            }
        }
    }

    /**
     * 批量删除team
     * @author cpy(1442435897@qq.com)
     * @return json
     */
    public  function  actionBatchDelInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Information();
            $params = $request->bodyParams;
            if(empty($params['ids'])) return code::error('error','参数错误');
            $data=$model->batchDelInfo($params);
            if(!empty($data['error'])){
                return code::error('error',$data['error']);
            }else{
                return code::success();
            }
        }
    }



}

